<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/10/30 0030
 * Time: 16:52
 */

namespace app\admin\controller;


use think\Controller;

class BannerMenu extends Controller
{
    public function index(){
        $menu = db("banner_menu")->select();
        $this->assign("menu",$menu);
        return view("index");
    }
    public function add(){
        if(request()->isAjax()){
            $file = request()->file('image');
            $info=$file->move( './static/images');
            $filename=str_replace("\\","/",$info->getSaveName());
            $image = '/static/images/'.$filename;
            $name = request()->param("name");
            $data = [
                'icon' => $image,
                'name' => $name,
                'addtime' => date("Y-m-d H:i:s",time())
            ];
            $row = db("banner_menu")->insert($data);
            if($row){
                return 200;
            }else{
                return -1;
            }
        }
        return view("add");
    }
    public function delete(){
        $id = input("id");
        $del = db("banner_menu")->delete($id);
        if($del){
            return 200;
        }else{
            return -1;
        }
    }
    public function edit(){
        if(request()->isAjax()){
            $id = input("id");
            $data = request()->param();
            $file = request()->file('image');
            if(empty($file)){
                $arr_data = [
                    'name' => $data['name'],
                    'icon' => $data['old_image'],
                    'addtime' => date("Y-m-d H:i:s",time())
                ];
            }else{
                $info=$file->move( './static/images');
                $filename=str_replace("\\","/",$info->getSaveName());
                $image = '/static/images/'.$filename;
                $arr_data = [
                    'name' => $data['name'],
                    'icon' => $image,
                    'addtime' => date("Y-m-d H:i:s",time())
                ];
            }
            $res = db("banner_menu")->where("id",$id)->update($arr_data);
            if($res){
                return 200;
            }else{
                return -1;
            }
        }
        $id = input("id");
        $data = db("banner_menu")->find($id);
        $this->assign("data",$data);
        return view("edit");
    }
}